package com.module.codeBind;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;

import base.util.Page;
import base.util.StringUtil;

import com.module.codeBind.manager.BatchCodeManager;
import com.module.codeBind.vo.BatchCode;
import com.module.codeBind.vo.BatchCodeItem;
import com.module.codeBind.vo.CodeBind;
import com.module.codeManage.setcode.manager.FirstCodeManager;
import com.module.product.ProductService;
import com.module.sys.LoginSession;



public class BatchCodeService {
	static Logger logger = Logger.getLogger(BatchCodeService.class);
	private ProductService productService;
	public BatchCodeService(){}
	/**
	 * 分页查询编号说明信息
	 * @param codeApp 批次属性对象
	 * @param start 开始分页行
	 * @param limit 每一页的最大行数
	 * @return 对象结果集
	 * @throws Exception
	 */
	 public Page toList(BatchCode bean,final Integer start,final Integer limit) throws Exception{
		   Page page = new Page();
           page.setStart(start);
           page.setLimit(limit);
           bean.setOrgId(LoginSession.getOrgids());
           BatchCodeManager batchCodeManager =BatchCodeManager.getInstance();
           page = batchCodeManager.toListBatchCode(bean,page);
           return page;
	}
	 /**
	  * 查询赋码物信息
	  * @return
	  */
	 public List getProductByOrgId(){
		 productService = new ProductService();
		 return productService.findProductListbyOrgId();
	 }
	 /**
	  * 根据ID查询对象
	  * @return 批次对象
	  * @throws Exception
	  */
	 public BatchCode findById(String id) throws Exception{
		BatchCode bean = new BatchCode();
	    BatchCodeManager batchCodeManager =BatchCodeManager.getInstance();
	    bean = batchCodeManager.findByIdBatchCode(id);
	    return bean;
		
	 }
	 
	 /**
	  * 保存批次
	  * @param appAttr 批次对象
	  * @return 成功或者失败
	  */
	 public Map<String,Object> save(BatchCode bean) throws Exception{
		 //验证批次
		FirstCodeManager firstCodeManager = FirstCodeManager.getInstance();
		Map<String ,Object> result = firstCodeManager.checkTag(bean);
		if(result!=null){
			 boolean state = (Boolean)result.get("state");
			 if(state){
				 List items = (List)result.get("items");
				 List<BatchCodeItem> itemsList = new ArrayList();

				 for(Object mapItems:items){
					 Map<String, Object>Item = (Map<String, Object>) mapItems;
					 BatchCodeItem codeItem = new BatchCodeItem();
					 codeItem.setEndSeq((Integer) Item.get("endSeq"));
					 codeItem.setStartSeq((Integer) Item.get("startSeq"));
					 codeItem.setVolumeNo((String) Item.get("volumeNo"));
					 codeItem.setEndVolumeCode((String) Item.get("endVolumeCode"));
					 codeItem.setStartVolumeCode((String) Item.get("startVolumeCode"));
					 itemsList.add(codeItem);
				 }
////////////////////////////BatchCodeItem类型
					bean.setItems(itemsList);
					bean.setOrgId(LoginSession.getOrgid());
				 if(StringUtil.isNull(bean.getId())){
					 bean.setCreaterId(LoginSession.getUserid());
				 }else {
					 bean.setUpdaterId(LoginSession.getUserid());
				 }
				 Map<String,Object> saveMap = new HashMap<String,Object>();
				 BatchCodeManager batchCodeManager =BatchCodeManager.getInstance();
				 saveMap = batchCodeManager.saveBatchCode(bean);
				 //添加BatchCode,BatchCodeItem操作记录
				 if(saveMap!=null){
						if((Boolean)saveMap.get("state")){
							bean = (BatchCode) saveMap.get("batchCode");
							result = firstCodeManager.updateTagBatchCode(bean);
							//FirstCode
						}else{
							result = saveMap;
						}
				 }
			 }
		}
		return result;	

	 }
	 
	/**
	 * 根据ID批量删除
	 * @param idArray ID数组 
	 * @return Map对象
	 * @throws Exception
	 */
	public Map<String,Object> remove(List idArray) throws Exception{
		 BatchCode bean = new BatchCode();
		 bean.setUpdaterId(LoginSession.getUserid());      
		 Map<String,Object> map = new HashMap<String,Object>();
		 
		 BatchCodeManager batchCodeManager =BatchCodeManager.getInstance();
		 map = batchCodeManager.deleteBatchCode(bean, idArray);
		 return map;
	 }
	/**
	 * 手工开启批次
	 * @param idArray Id数组
	 * @return Map对象
	 * @throws Exception
	 */
	public Map<String,Object> openBatchByHand(List idArray) throws Exception {
		 Map<String,Object> map = new HashMap<String,Object>();
		
	  	 BatchCodeManager batchCodeManager =BatchCodeManager.getInstance();
	  	 map = batchCodeManager.openBatchByHand(idArray);
	  	 return map;
	}
	/**
	 * 根据商品获取批次号
	 * @return 返回属性结果集
	 * @throws Exception
	 */
	public List comboList(String productId,String bindId) throws Exception{
//		Map<String,Object> map = new HashMap<String,Object>();
		BatchCode bean = new BatchCode();
		bean.setProductId(productId);
		CodeBind bind = new CodeBind();
		bind.setId(bindId);
		BatchCodeManager batchCodeManager =BatchCodeManager.getInstance();
		List list = batchCodeManager.comboListBatchCode(bean, bind);
		JSONArray jsonArray = JSONArray.fromObject(list); 

		return jsonArray;
		
	}
//  
//public ProductService getProductService() {
//	return productService;
//}
//public void setProductService(ProductService productService) {
//	this.productService = productService;
//}
}
